package operation;

import book.Book;
import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 74646
 * Date: 2022-02-10
 * Time: 16:33
 */
public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        System.out.println("请输入要删除的书名：");
        String name = scanner.nextLine();
        int size = bookList.getUserSize();
        int index = 0;
        int i = 0;
        for (; i < size; i++) {
            Book book = bookList.getPos(i);
            if(name.equals(book.getName())){
                index = i;
                break;
            }
        }
        if(i>=size){
            System.out.println("书架上没有要删除的书");
            return;
        }
        for (int j = index; j < size-1; j++) {
            // j + 1 位置的书放到 j 位置
            Book book = bookList.getPos(j+1);
            bookList.setBook(j,book);
        }
        bookList.setBook(size,null);
        bookList.setUserSize(size-1);
        System.out.println("删除成功！");
    }
}
